
// Source File Name:   MOImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.common.VersionInfo;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.MO;
import com.sunwayhorizo.health.hl7.datatypes.QTY;
import com.sunwayhorizo.health.hl7.datatypes.REAL;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;


// Referenced classes of package oracle.apps.ctb.hl7.types.common:
//            ANYImpl, BLImpl, CSImpl, NullFlavorImpl, 
//            REALImpl, STImpl, TYPEImpl

public class MOImpl extends ANYImpl
    implements MO
{

    private REAL mValue;
    private CS mCurrency;
    private static final MO nullNI = new MOImpl(CSImpl.valueOf("NI"));
    private static final MO nullOTH = new MOImpl(CSImpl.valueOf("OTH"));
    private static final MO nullNINF = new MOImpl(CSImpl.valueOf("NINF"));
    private static final MO nullPINF = new MOImpl(CSImpl.valueOf("PINF"));
    private static final MO nullUNK = new MOImpl(CSImpl.valueOf("UNK"));
    private static final MO nullASKU = new MOImpl(CSImpl.valueOf("ASKU"));
    private static final MO nullNAV = new MOImpl(CSImpl.valueOf("NAV"));
    private static final MO nullMSK = new MOImpl(CSImpl.valueOf("MSK"));
    private static final MO nullNA = new MOImpl(CSImpl.valueOf("NA"));
    private static final MO nullNASK = new MOImpl(CSImpl.valueOf("NASK"));

    protected MOImpl(REAL real, CS cs)
    {
        if(real == null || cs == null)
        {
            if(real == null)
                throw ANYImpl.newNullException("MO", "constructor", "value");
            if(cs == null)
                throw ANYImpl.newNullException("MO", "constructor", "currency");
        }
        mValue = real;
        mCurrency = cs;
    }

    protected MOImpl(CS cs)
    {
        super(true, cs);
        mValue = REALImpl.nullREAL(NullFlavorImpl.NI);
        mCurrency = CSImpl.nullCS(NullFlavorImpl.NI);
    }

    public static MO valueOf(REAL real, CS cs)
    {
        return new MOImpl(real, cs);
    }

    public static MO nullMO()
    {
        return new MOImpl(null);
    }

    public BL equal(ANY any)
    {
        if(any == this)
            return BLImpl.TRUE;
        if(!(any instanceof MO))
            return BLImpl.FALSE;
        if(isNull().or(any.isNull()).isTrue())
        {
            return BLImpl.FALSE;
        } else
        {
            MO mo = (MO)any;
            BL bl = mCurrency.equal(mo.currency());
            bl = bl.and(BLImpl.valueOf(mValue.equal(mo.value()).isTrue()));
            return bl;
        }
    }

    public TYPE dataType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.MO;
    }

    public BL compares(QTY qty)
    {
        BLImpl blimpl = null;
        if(qty == null || isNull().or(qty.isNull()).isTrue())
            blimpl = BLImpl.FALSE;
        else
        if(qty.dataType().equal(com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.MO).isTrue())
        {
            MO mo = (MO)qty;
            if(mCurrency.equal(mo.currency()).isTrue())
                blimpl = BLImpl.TRUE;
        } else
        {
            blimpl = BLImpl.FALSE;
        }
        return blimpl;
    }

    public BL lessOrEqual(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("MO", "lessOrEqual");
    }

    public MO plus(MO mo)
    {
        throw ANYImpl.newUnsupportedException("MO", "plus");
    }

    public TYPE diffType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.MO;
    }

    public QTY minus(QTY qty)
    {
        if(compares(qty).isFalse())
        {
            return nullMO(NullFlavorImpl.NA);
        } else
        {
            MO mo = (MO)qty;
            REAL real = (REAL)mValue.minus(mo.value());
            return new MOImpl(real, mCurrency);
        }
    }

    public QTY plus(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("MO", "plus");
    }

    public BL isZero()
    {
        throw ANYImpl.newUnsupportedException("MO", "isZero");
    }

    public BL lessThan(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("MO", "lessThan");
    }

    public BL greaterOrEqual(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("MO", "greaterOrEqual");
    }

    public BL greaterThan(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("MO", "greaterThan");
    }

    public MO times(REAL real)
    {
        throw ANYImpl.newUnsupportedException("MO", "times");
    }

    public MO negated()
    {
        throw ANYImpl.newUnsupportedException("MO", "negated");
    }

    public REAL value()
    {
        return mValue;
    }

    public CS currency()
    {
        return mCurrency;
    }

    public ST literal()
    {
        if(isNull().isTrue())
            return STImpl.nullST(nullFlavor());
        else
            return STImpl.valueOf(mCurrency.literal().toString() + " " + mValue.toString());
    }

    public String toString()
    {
        if(isNull().isTrue())
            return "<Null:" + nullFlavor().toString() + ">";
        else
            return mCurrency.toString() + " " + mValue.toString();
    }

    public int hashCode()
    {
        int i = 17;
        if(isNull().isFalse())
        {
            i = 37 * i + value().hashCode();
            i = 37 * i + currency().hashCode();
        }
        return i;
    }

//    public XMLNode[] writeXML(XMLDocument xmldocument, String s)
//        throws Hl7Exception
//    {
//        try
//        {
//            XMLElement xmlelement = null;
//            if(value() != null && value().isNull().isFalse())
//            {
//                xmlelement = (XMLElement)xmldocument.createElement(s);
//                xmlelement.setAttribute("value", value().stringValue());
//            }
//            if(currency() != null && currency().isNull().isFalse())
//                xmlelement.setAttribute("currency", currency().code().stringValue());
//            return (new XMLNode[] {
//                xmlelement
//            });
//        }
//        catch(Exception exception)
//        {
//            throw new Hl7Exception(exception);
//        }
//    }

    public static MO nullMO(CS cs)
    {
        String s = null;
        if(cs == null || cs.literal().stringValue() == null || cs.isNull().isTrue())
            s = "NI";
        else
            s = cs.literal().stringValue();
        if(s.equals("NI"))
            return nullNI;
        if(s.equals("OTH"))
            return nullOTH;
        if(s.equals("NINF"))
            return nullNINF;
        if(s.equals("PINF"))
            return nullPINF;
        if(s.equals("UNK"))
            return nullUNK;
        if(s.equals("ASKU"))
            return nullASKU;
        if(s.equals("NAV"))
            return nullNAV;
        if(s.equals("MSK"))
            return nullMSK;
        if(s.equals("NA"))
            return nullNA;
        if(s.equals("NASK"))
            return nullNASK;
        else
            return nullNI;
    }

}
